package org.sky.service.serviceImpl;

import org.sky.base.DaoSupport;
import org.sky.model.Meal;
import org.sky.service.MealService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-10-23
 * Time: 下午1:51
 * To change this template use FileUtil | Settings | FileUtil Templates.
 */

@Service
@Transactional
public class MealServiceImpl extends DaoSupport<Meal> implements MealService {
    @Override
    public List<Meal> findMealsFromTime(Time time) {
        Query query = em.createQuery("select m from Meal m where m.beginTime <= ?1 and m.endTime >= ?1");
        query.setParameter(1, time);
        return query.getResultList();
    }

    /**
     * 查找房间价格
     *
     * @param roomStyle 房间类型
     * @param mealName  套餐名字
     * @param holiday   节假日
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    public double findPriceByRoomStyleAndMeal(String roomStyle, String mealName, String holiday, Time beginTime, Time endTime) {
        Query query = em.createQuery("select m from Meal m where m.mealName = ?1 and m.holiday = ?2 and m.beginTime = ?3 and m.endTime = ?4");
        query.setParameter(1, mealName).setParameter(2, holiday).setParameter(3, beginTime).setParameter(4, endTime);
        List<Meal> rus = query.getResultList();

        if (rus.size() <= 0)
            return 0.0d;
        Meal ru = rus.get(0);


        if ("情侣房".equals(roomStyle)) {
            return ru.getCoupleRoomPrice();
        } else if ("标准房".equals(roomStyle)) {
            return ru.getStandardRoomPrice();
        } else if ("中房".equals(roomStyle)) {
            return ru.getMiddleRoomPrice();
        } else if ("大房".equals(roomStyle)) {
            return ru.getBigRoomPrice();
        } else if ("VIP房".equals(roomStyle)) {
            return ru.getVipRoomPrice();
        } else return 0.0d;

    }

    /**
     * 修改房价
     *
     * @param roomStyle 房间类型
     * @param mealName  套餐名字
     * @param price     价格
     * @param holiday   节假日
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    public boolean setPrice(String roomStyle, String mealName, double price, String holiday, Time beginTime, Time endTime) {
        Query query = em.createQuery("SELECT from Meal m where m.mealName = ?1 and m.holiday = ?2 and m.beginTime = ?3 and m.endTime = ?4");
        query.setParameter(1, mealName).setParameter(2, holiday).setParameter(3, beginTime).setParameter(4, endTime);
        List<Meal> rus = query.getResultList();
        if (rus.size() <= 0)
            return false;
        Meal ru = rus.get(0);

        if ("情侣房".equals(roomStyle)) {
            ru.setCoupleRoomPrice(price);
        } else if ("标准房".equals(roomStyle)) {
            ru.setStandardRoomPrice(price);
        } else if ("中房".equals(roomStyle)) {
            ru.setMiddleRoomPrice(price);
        } else if ("大房".equals(roomStyle)) {
            ru.setBigRoomPrice(price);
        } else if ("VIP房".equals(roomStyle)) {
            ru.setVipRoomPrice(price);
        } else
            return false;

        try {
            update(ru);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public List<Meal> allMeal() {
        Query query = em.createQuery("SELECT from Meal m");
        return query.getResultList();
    }

    /**
     * 查询某种房间类型的价格
     *
     * @param roomStyle 房间类型
     * @return
     */
    public List<Meal> findRoomStylePrice(String roomStyle) {
        return null;
    }


}
